package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IMessageActivityModel;
import com.wgts.wgts.mvp.presenter.IMessageActivityPresenter;
import com.wgts.wgts.mvp.view.IMessageActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.MessageListEntity;
import com.wgts.wgts.netmodle.resp.UnreadMessageEntity;

import java.util.List;

/*

 *
 *   data:2018/10/2215:17
 *
 */public class MessageActivityPresenter implements IMessageActivityPresenter {
     private IMessageActivityView view;
     private IMessageActivityModel model;
     private int pageIndex = 1;
     private int pageSize = 20;

    public MessageActivityPresenter(IMessageActivityView view, IMessageActivityModel model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void unreadMessage(String typeCodes) {
        model.unreadMessage(typeCodes, new HttpResultSubscriber<List<UnreadMessageEntity>>() {
            @Override
            public void onSuccess(List<UnreadMessageEntity> unreadMessageEntities) {
                view.unreadMessage(unreadMessageEntities);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void clearMessage(final String typeCodes) {
        model.clearMessage(typeCodes, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.cleadMessage(typeCodes);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void initList() {
        pageIndex = 1;
        model.initList(pageIndex, pageSize, new HttpResultSubscriber<List<MessageListEntity>>() {
            @Override
            public void onSuccess(List<MessageListEntity> entities) {
                if (entities.size() < pageSize){
                    view.initMessageList(entities, true);
                }else {
                    view.initMessageList(entities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void loadMore() {
        model.initList(pageIndex, pageSize, new HttpResultSubscriber<List<MessageListEntity>>() {
            @Override
            public void onSuccess(List<MessageListEntity> entities) {
                if (entities.size() < pageSize){
                    view.loadMore(entities, true);
                }else {
                    view.loadMore(entities, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }
}
